{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "b4f87d24",
   "metadata": {},
   "source": [
    "## 离散傅里叶变换DFT"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3ba806ba",
   "metadata": {},
   "source": [
    "离散傅里叶变换是对离散傅里叶级数的周期序列取主值，因为离散傅里叶级数虽是周期序列却只有 个独立的复值，只要知道它的一个周期的内容，其它的内容也就知道了。\n",
    "如果同时限制时域和频域中的$n$都只在区间$[0,N-1]$内取值,就得到了一个周期的$x(n)$和一个周期的$X(k)$间的对应关系\n",
    "$$\n",
    "\\begin{equation}\n",
    "X(k)=DFT[x(n)]=\\sum_{n=0}^{N-1}x(n)W_N^{kn}\\qquad 0\\leq k\\leq N-1 \\qquad(1)\n",
    "\\\\X(k)=IDFT[x(k)]=\\frac{1}{N}\\sum_{k=0}^{N-1}x(k)W_N^{-kn}\\qquad 0\\leq n\\leq N-1 \\qquad(2)\n",
    "\\end{equation}\n",
    "$$\n",
    "上两式即称为有限长序列的离散傅里叶变换对。(1)式称为离散傅里叶变换，简称DFT；(2)式称为离散傅里叶逆变换，简称为IDFT。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1f650552",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "f704ab54",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 2.5+0.00000000e+00j -0.5+8.66025404e-01j -0.5+2.88675135e-01j\n",
      " -0.5-3.06161700e-16j -0.5-2.88675135e-01j -0.5-8.66025404e-01j]\n",
      "[ 0.41666667+0.00000000e+00j -0.08333333-1.44337567e-01j\n",
      " -0.08333333-4.81125224e-02j -0.08333333+5.10269500e-17j\n",
      " -0.08333333+4.81125224e-02j -0.08333333+1.44337567e-01j]\n"
     ]
    }
   ],
   "source": [
    "#libraries\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "import math\n",
    "import numpy as np\n",
    "\n",
    "# Number of sample points\n",
    "N = 6\n",
    "# sample spacing\n",
    "T = 1.0 / 6.0\n",
    "x = np.linspace(0.0, N*T, N, endpoint=False)\n",
    "ys = x\n",
    "\n",
    "#build signal：Create a function to calculate the composite matrix M\n",
    "def synthesis_matrix(N):\n",
    "    ts = np.arange(N)/N\n",
    "    fs = np.arange(N)\n",
    "    args = np.outer(ts,fs)\n",
    "    M = np.exp(1j*2*math.pi*args)\n",
    "    return M\n",
    "\n",
    "#Define DFT positive transformation\n",
    "def dft(ys):\n",
    "    N = len(ys)\n",
    "    M = synthesis_matrix(N)\n",
    "    amps = M.conj().transpose().dot(ys)#Calculate the weighted sum of frequency elements\n",
    "    return amps\n",
    "\n",
    "#Define inverse DFT transform\n",
    "def idft(ys):\n",
    "    N = len(ys)\n",
    "    M = synthesis_matrix(N)\n",
    "    amps = M.dot(ys)/N\n",
    "    return amps\n",
    "\n",
    "print(dft(ys))\n",
    "print(idft(ys))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
